/*
 * @Author: your name
 * @Date: 2021-11-11 14:27:48
 * @LastEditTime: 2021-11-16 16:38:50
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \epet_back\routes\newRouter.js
 */
const Router = require('koa-router')
const NewRouter = new Router()
const query = require('../utils/query')
let qn = require('../utils/may');  //导入七牛文件   上方组件文件我命名qiniu.js
const fs = require('fs');
NewRouter.get('/get',async ctx => {
    const sql = `select * from navbtn`
    let result = await query(sql)
    if(result.length > 0){
        ctx.body = {
            code: 200,
            msg: '获取导航按钮成功',
            data: result
        }
    }else{
        ctx.body = {
            code: 201,
            msg: '无相关数据',
            data: []
        }
    }
})
let data = {}
let result = (obj, bool) => {
    return { status: bool ? 0 : 1, data: obj }
}
 function Datas (file){
    qn.update(file,  (res, routerRes)=>{
        data = res
    console.log('data')
    console.log(data)      // 这里可以拿到，但是执行
        return data
    })
}
 NewRouter.post('/update',  async (ctx, next) => {
        const file = ctx.request.files.file
        await  Datas(file); //  result为空
        console.log('result')
        console.log(data)
        if(data){
            ctx.body = {
                code: 200,
                msg: '成功',
                data: data
            }
        }else{
            ctx.body = {
                code: 201,
                msg: '无相关数据',
                data: []
            }
        }
        // setTimeout(() => {
        //     console.log('999')
        //     ctx.body = data
        // }, 1000);
    // console.log(data) {}
    // if(result.length > 0){
    //     ctx.body = {
    //         code: 200,
    //         msg: '获取导航按钮成功',
    //         data: result
    //     }
    // }else{
    //     ctx.body = {
    //         code: 201,
    //         msg: '无相关数据',
    //         data: []
    //     }
    // }
})
module.exports = NewRouter
